package com.meitianhui.convenitenbuy.module.order.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.order.model.bean.HomeServiceGood;
import com.meitianhui.convenitenbuy.module.order.model.bean.HomeServiceGoodDetail;
import com.meitianhui.convenitenbuy.module.order.model.bean.OrderInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/16.
 */

public class HomeServiceOrderAdapter extends BaseQuickAdapter<OrderInfo.ListBean, BaseViewHolder> {
    private Context mContext;
    private Map<String, String> mUrlMap;
    private OnClickOrder mOnClickOrder;

    public HomeServiceOrderAdapter(Context context, @Nullable List<OrderInfo.ListBean> data, Map<String, String> urlMap, OnClickOrder onClickOrder) {
        super(R.layout.item_home_service_order, data);
        mContext = context;
        mUrlMap = urlMap;
        mOnClickOrder = onClickOrder;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderInfo.ListBean item) {
        Button btnCancel = helper.getView(R.id.btn_cancel);
        Button btnConfirm = helper.getView(R.id.btn_confirm);
        TextView tvStatus = helper.getView(R.id.tv_status);
        switch (item.getType()){

            case "1":    //本地特卖
                RecyclerView recyclerView = helper.getView(R.id.rv_shop);
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                recyclerView.setLayoutManager(layoutManager);
                RxView.visibility(helper.getView(R.id.rv_shop)).call(true);
                RxView.visibility(helper.getView(R.id.ll_shop)).call(false);
                if (item.getStatus().equals(Constants.PC__ORDER_PAYED) || item.getStatus().equals(Constants.PC_ORDER_DELIVERED) || item.getStatus().equals(Constants.PC__ORDER_ASSIGNED)) {//已支付  已发货
                    RxView.visibility(helper.getView(R.id.rl_bottom)).call(true);
                    if (item.getStatus().equals(Constants.PC__ORDER_PAYED)) {
                        tvStatus.setText("已支付");
                        btnCancel.setVisibility(View.VISIBLE);
                        btnConfirm.setVisibility(View.GONE);
                    } else if (item.getStatus().equals(Constants.PC_ORDER_DELIVERED)) {
                        tvStatus.setText("已发货");
                        btnCancel.setVisibility(View.GONE);
                        btnConfirm.setVisibility(View.VISIBLE);
                    } else if (item.getStatus().equals(Constants.PC__ORDER_ASSIGNED)) {
                        tvStatus.setText("配送中");
                        btnCancel.setVisibility(View.GONE);
                        btnConfirm.setVisibility(View.VISIBLE);
                    }
                } else {
                    btnCancel.setVisibility(View.VISIBLE);
                    if (item.getStatus().equals(Constants.PC__ORDER_RECEIVED)) {
                        tvStatus.setText("已收货");
                    } else if (item.getStatus().equals(Constants.PC__ORDER_CANCELLED)) {
                        tvStatus.setText("已取消");
                    } else {
                        tvStatus.setText("已收货");
                    }
                    RxView.visibility(helper.getView(R.id.rl_bottom)).call(false);
                }
                if (!TextUtils.isEmpty(item.getGoods_list())) {
                    List<HomeServiceGood> homeServiceGoods=new ArrayList<>();
                    try {
                        JSONArray jsonArray=new JSONArray(item.getGoods_list());
                        for (int i=0;i<jsonArray.length();i++){
                            JSONObject jsonObject=jsonArray.getJSONObject(i);
                            Gson gson = new Gson();
                            HomeServiceGood homeServiceGood = gson.fromJson(jsonObject.toString(), HomeServiceGood.class);
                            homeServiceGoods.add(homeServiceGood);
                        }
                        if(homeServiceGoods.size()>0) {
                            HomeServiceGoodAdapter adapter = new HomeServiceGoodAdapter(homeServiceGoods,mUrlMap);
                            recyclerView.setAdapter(adapter);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                RxView.visibility(helper.getView(R.id.iv_sale)).call(false);
                helper.setText(R.id.tv_store_address, item.getStores_name());
                helper.setText(R.id.tv_order_number, item.getOrder_no());
                helper.setText(R.id.tv_order_pay_type, item.getPayType());
                helper.setText(R.id.tv_name, item.getContact_person());
                helper.setText(R.id.tv_phone, item.getContact_tel());
                helper.setText(R.id.tv_address, item.getAddress());
                helper.setText(R.id.tv_good_num, "共" + item.getItem_num() + "件商品");
                helper.setText(R.id.tv_good_fee, item.getTotal_fee());
                helper.setText(R.id.tv_create_date, item.getOrder_date());
                //取消订单
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickOrder != null) {
                            mOnClickOrder.localCancelClick(item.getOrder_id());
                        }
                    }
                });
                //确认收货
                btnConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickOrder != null) {
                            mOnClickOrder.localConfirmClick(item.getOrder_id());
                        }
                    }
                });
                helper.setOnClickListener(R.id.tv_store_phone, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //用intent启动拨打电话
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + item.getContact_tel()));
                        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            return;
                        }
                        mContext.startActivity(intent);
                    }
                });
                String remark = item.getRemark();
                if (TextUtils.isEmpty(remark)) {
                    RxView.visibility(helper.getView(R.id.ll_remark)).call(false);
                } else {
                    RxView.visibility(helper.getView(R.id.ll_remark)).call(true);
                    RxTextView.text(helper.getView(R.id.tv_remark)).call(remark);
                }
                String fee = item.getRebate_fee();
                if (!TextUtils.isEmpty(fee) && !fee.equals("0")
                        && !fee.equals("0.00") && !fee.equals("0.0")) {
                    helper.setText(R.id.tv_good_freight,"返¥" + fee);
                } else {
                    helper.setText(R.id.tv_good_freight,"");
                }
                if (TextUtils.isEmpty(item.getLoaded_code())) {
                    RxView.visibility(helper.getView(R.id.ll_code)).call(false);
                } else {
                    RxView.visibility(helper.getView(R.id.ll_code)).call(true);
                    helper.setText(R.id.tv_code,item.getLoaded_code());
                }
                break;
            case "2":     //团购预售
                RxView.visibility(helper.getView(R.id.iv_sale)).call(true);
                RxView.visibility(helper.getView(R.id.rv_shop)).call(false);
                RxView.visibility(helper.getView(R.id.ll_shop)).call(true);
                if (item.getStatus().equals(Constants.PC__ORDER_PAYED_HPT) || item.getStatus().equals(Constants.PC_ORDER_ARRIVED)) {//已支付  已发货
                    RxView.visibility(helper.getView(R.id.rl_bottom)).call(true);
                    if (item.getStatus().equals(Constants.PC__ORDER_PAYED_HPT)) {
                        tvStatus.setText("已支付");
                        btnCancel.setVisibility(View.VISIBLE);
                        btnConfirm.setVisibility(View.GONE);
                    } else if (item.getStatus().equals(Constants.PC_ORDER_ARRIVED)) {
                        tvStatus.setText("已到货");
                        btnCancel.setVisibility(View.GONE);
                        btnConfirm.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (item.getStatus().equals(Constants.PC__ORDER_RECEIVED)) {
                        tvStatus.setText("已收货");
                    } else if (item.getStatus().equals(Constants.PC__ORDER_CANCELLED)) {
                        tvStatus.setText("已取消");
                    } else if (item.getStatus().equals(Constants.PC_ORDER_DELIVERED)) {
                        tvStatus.setText("已发货");
                    } else {
                        tvStatus.setText("未知");
                    }
                    RxView.visibility(helper.getView(R.id.rl_bottom)).call(false);
                }
                helper.setText(R.id.tv_store_address, item.getStores_name());
                helper.setText(R.id.tv_order_number, item.getOrder_no());
                helper.setText(R.id.tv_order_pay_type, item.getPayType());
                helper.setText(R.id.tv_name, item.getContact_person());
                helper.setText(R.id.tv_phone, item.getContact_tel());
                helper.setText(R.id.tv_address, item.getAddress());
                helper.setText(R.id.tv_good_num, "共" + item.getItem_num() + "件商品");
                helper.setText(R.id.tv_good_fee, item.getTotal_fee());
                helper.setText(R.id.tv_create_date, item.getOrder_date());
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickOrder != null) {
                            mOnClickOrder.groupCancelClick(item.getOrder_id());
                        }
                    }
                });
                btnConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickOrder != null) {
                            mOnClickOrder.groupConfirmClick(item.getOrder_id());
                        }
                    }
                });
                helper.setOnClickListener(R.id.tv_store_phone, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //用intent启动拨打电话
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + item.getContact_tel()));
                        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            return;
                        }
                        mContext.startActivity(intent);
                    }
                });
                String url = "";
                HomeServiceGoodDetail homeServiceGoodDetail;
                if (mUrlMap != null) {
                    String s_pic_info = item.getGoods_list();
                    try {
                        JSONArray jsonArray = new JSONArray(s_pic_info);
                        if (jsonArray.length() > 0) {
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            Gson gson = new Gson();
                            homeServiceGoodDetail = gson.fromJson(jsonObject.toString(), HomeServiceGoodDetail.class);
                            if (homeServiceGoodDetail != null) {
                                url = mUrlMap.get(homeServiceGoodDetail.getPath_id());
                                helper.setText(R.id.tv_good_name,homeServiceGoodDetail.getGoods_title());
                                helper.setText(R.id.tv_good_price,"¥" + homeServiceGoodDetail.getSale_price());
                                helper.setText(R.id.tv_good_num2,"x" + homeServiceGoodDetail.getQty());
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                GlideUtils.setCircleImage(mContext,url,R.mipmap.wallet_coupon_item_default,R.mipmap.wallet_coupon_item_default,helper.getView(R.id.iv_good));
                if (TextUtils.isEmpty(item.getLoaded_code())) {
                    RxView.visibility(helper.getView(R.id.ll_code)).call(false);
                } else {
                    RxView.visibility(helper.getView(R.id.ll_code)).call(true);
                    helper.setText(R.id.tv_code,item.getLoaded_code());
                }
                break;
        }
    }


    public void setNewData(List data, Map<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }

    public void addData(List data, Map<String, String> map) {
        addData(data);
        mUrlMap.putAll(map);
    }

    public interface OnClickOrder {
        void localConfirmClick(String orderId);
        void localCancelClick(String orderId);

        void groupConfirmClick(String orderId);
        void groupCancelClick(String orderId);

    }

}
